<!DOCTYPE html><html lang="en"><head><title>PaymentWithPayPalWithWebExperienceProfile.aspx</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content=""><meta name="groc-document-path" content="PaymentWithPayPalWithWebExperienceProfile.aspx"><meta name="groc-project-path" content="PaymentWithPayPalWithWebExperienceProfile.aspx.cs"><link rel="stylesheet" type="text/css" media="all" href="assets/style.css"><script type="text/javascript" src="assets/behavior.js"></script><body><div id="meta"><div class="file-path">PaymentWithPayPalWithWebExperienceProfile.aspx.cs</div></div><div id="document"><div class="segment"><div class="code"><div class="wrapper">﻿<span class="hljs-keyword">using</span> PayPal.Api;
<span class="hljs-keyword">using</span> PayPal.Sample.Utilities;
<span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Collections.Generic;
<span class="hljs-keyword">using</span> System.Linq;
<span class="hljs-keyword">using</span> System.Web;
<span class="hljs-keyword">using</span> System.Web.UI;
<span class="hljs-keyword">using</span> System.Web.UI.WebControls;

namespace PayPal.Sample
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">partial</span> <span class="hljs-keyword">class</span> PaymentWithPayPalWithWebExperienceProfile : BaseSamplePage
    {
        <span class="hljs-keyword">protected</span> <span class="hljs-keyword">override</span> <span class="hljs-keyword">void</span> <span class="hljs-title">RunSample</span>()
        {</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><h3 id="api-context">Api Context</h3>
<p>Pass in a <code>APIContext</code> object to authenticate 
the call and to send a unique request id 
(that ensures idempotency). The SDK generates
a request id if you do not pass one explicitly. 
See <a href="/Source/Configuration.html">Configuration.cs</a> to know more about APIContext.</p></div></div><div class="code"><div class="wrapper">            <span class="hljs-keyword">var</span> apiContext = Configuration.GetAPIContext();

            <span class="hljs-keyword">string</span> payerId = Request.Params[<span class="hljs-string">"PayerID"</span>];
            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">string</span>.IsNullOrEmpty(payerId))
            {</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Create the web experience profile</p></div></div><div class="code"><div class="wrapper">                <span class="hljs-keyword">var</span> profile = <span class="hljs-keyword">new</span> WebProfile
                {
                    name = Guid.NewGuid().ToString(),
                    presentation = <span class="hljs-keyword">new</span> Presentation
                    {
                        brand_name = <span class="hljs-string">"PayPal .NET SDK"</span>,
                        locale_code = <span class="hljs-string">"US"</span>,
                        logo_image = <span class="hljs-string">"https://raw.githubusercontent.com/wiki/paypal/PayPal-NET-SDK/images/homepage.jpg"</span>
                    },
                    input_fields = <span class="hljs-keyword">new</span> InputFields
                    {
                        no_shipping = <span class="hljs-number">1</span>
                    }
                };</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">                // Ignore workflow code segment</span></div><div class="wrapper">                <span class="hljs-comment">// Ignore workflow code segment</span>
                <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
                <span class="hljs-keyword">this</span>.flow.AddNewRequest(<span class="hljs-string">"Create new web experience profile (NOTE: This only needs to be done once)"</span>, profile);
                <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span>

                <span class="hljs-keyword">var</span> createdProfile = profile.Create(apiContext);</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">                // Ignore workflow code segment</span></div><div class="wrapper">                <span class="hljs-comment">// Ignore workflow code segment</span>
                <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
                <span class="hljs-keyword">this</span>.flow.RecordResponse(createdProfile);
                <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Setup the redirect URI to use. The guid value is used to keep the flow information.</p></div></div><div class="code"><div class="wrapper">                <span class="hljs-keyword">var</span> baseURI = Request.Url.Scheme + <span class="hljs-string">"://"</span> + Request.Url.Authority + <span class="hljs-string">"/PaymentWithPayPal.aspx?"</span>;
                <span class="hljs-keyword">var</span> guid = Convert.ToString((<span class="hljs-keyword">new</span> Random()).Next(<span class="hljs-number">100000</span>));
                baseURI += <span class="hljs-string">"guid="</span> + guid + <span class="hljs-string">"&amp;webProfileId="</span> + createdProfile.id;</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Create the payment</p></div></div><div class="code"><div class="wrapper">                <span class="hljs-keyword">var</span> payment = <span class="hljs-keyword">new</span> Payment
                {
                    intent = <span class="hljs-string">"sale"</span>,
                    experience_profile_id = createdProfile.id,
                    payer = <span class="hljs-keyword">new</span> Payer
                    {
                        payment_method = <span class="hljs-string">"paypal"</span>
                    },
                    transactions = <span class="hljs-keyword">new</span> List&lt;Transaction&gt;
                {
                    <span class="hljs-keyword">new</span> Transaction
                    {
                        description = <span class="hljs-string">"Ticket information."</span>,
                        item_list = <span class="hljs-keyword">new</span> ItemList
                        {
                            items = <span class="hljs-keyword">new</span> List&lt;Item&gt;
                            {
                                <span class="hljs-keyword">new</span> Item
                                {
                                    name = <span class="hljs-string">"Concert ticket"</span>,
                                    currency = <span class="hljs-string">"USD"</span>,
                                    price = <span class="hljs-string">"20.00"</span>,
                                    quantity = <span class="hljs-string">"2"</span>,
                                    sku = <span class="hljs-string">"ticket_sku"</span>
                                }
                            }
                        },
                        amount = <span class="hljs-keyword">new</span> Amount
                        {
                            currency = <span class="hljs-string">"USD"</span>,
                            total = <span class="hljs-string">"45.00"</span>,
                            details = <span class="hljs-keyword">new</span> Details
                            {
                                tax = <span class="hljs-string">"5.00"</span>,
                                subtotal = <span class="hljs-string">"40.00"</span>
                            }
                        }
                    }
                },
                    redirect_urls = <span class="hljs-keyword">new</span> RedirectUrls
                    {
                        return_url = baseURI + <span class="hljs-string">"&amp;return=true"</span>,
                        cancel_url = baseURI + <span class="hljs-string">"&amp;cancel=true"</span>
                    }
                };</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">                // Ignore workflow code segment</span></div><div class="wrapper">                <span class="hljs-comment">// Ignore workflow code segment</span>
                <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
                <span class="hljs-keyword">this</span>.flow.AddNewRequest(<span class="hljs-string">"Create PayPal payment"</span>, payment);
                <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span>

                <span class="hljs-keyword">var</span> createdPayment = payment.Create(apiContext);</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">                // Ignore workflow code segment</span></div><div class="wrapper">                <span class="hljs-comment">// Ignore workflow code segment</span>
                <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
                <span class="hljs-keyword">this</span>.flow.RecordResponse(createdPayment);
                <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Use the returned payment&#39;s approval URL to redirect the buyer to PayPal and approve the payment.</p></div></div><div class="code"><div class="wrapper">                <span class="hljs-keyword">var</span> approvalUrl = createdPayment.GetApprovalUrl();

                <span class="hljs-keyword">this</span>.flow.RecordRedirectUrl(<span class="hljs-string">"Redirect to PayPal to approve the payment..."</span>, approvalUrl);
                Session.Add(guid, createdPayment.id);
                Session.Add(<span class="hljs-string">"flow-"</span> + guid, <span class="hljs-keyword">this</span>.flow);
            }
            <span class="hljs-keyword">else</span>
            {
                <span class="hljs-keyword">var</span> guid = Request.Params[<span class="hljs-string">"guid"</span>];
                <span class="hljs-keyword">var</span> webProfileId = Request.Params[<span class="hljs-string">"webProfileId"</span>];
                <span class="hljs-keyword">var</span> isReturnSet = Request.Params[<span class="hljs-string">"return"</span>];</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">                // Ignore workflow code segment</span></div><div class="wrapper">                <span class="hljs-comment">// Ignore workflow code segment</span>
                <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
                <span class="hljs-keyword">this</span>.flow = Session[<span class="hljs-string">"flow-"</span> + guid] <span class="hljs-keyword">as</span> RequestFlow;
                <span class="hljs-keyword">this</span>.RegisterSampleRequestFlow();
                <span class="hljs-keyword">this</span>.flow.RecordApproval(<span class="hljs-string">"PayPal payment approved successfully."</span>);
                <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span>

                <span class="hljs-keyword">if</span> (<span class="hljs-keyword">string</span>.IsNullOrEmpty(isReturnSet))
                {</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">                    // Ignore workflow code segment</span></div><div class="wrapper">                    <span class="hljs-comment">// Ignore workflow code segment</span>
                    <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
                    <span class="hljs-keyword">this</span>.flow.RecordApproval(<span class="hljs-string">"PayPal payment canceled by buyer."</span>);
                    <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span>
                }
                <span class="hljs-keyword">else</span>
                {</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">                    // Ignore workflow code segment</span></div><div class="wrapper">                    <span class="hljs-comment">// Ignore workflow code segment</span>
                    <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
                    <span class="hljs-keyword">this</span>.flow.RecordApproval(<span class="hljs-string">"PayPal payment approved successfully."</span>);
                    <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Using the information from the redirect, setup the payment to execute.</p></div></div><div class="code"><div class="wrapper">                    <span class="hljs-keyword">var</span> paymentId = Session[guid] <span class="hljs-keyword">as</span> <span class="hljs-keyword">string</span>;
                    <span class="hljs-keyword">var</span> paymentExecution = <span class="hljs-keyword">new</span> PaymentExecution() { payer_id = payerId };
                    <span class="hljs-keyword">var</span> payment = <span class="hljs-keyword">new</span> Payment() { id = paymentId };</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">                    // Ignore workflow code segment</span></div><div class="wrapper">                    <span class="hljs-comment">// Ignore workflow code segment</span>
                    <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
                    <span class="hljs-keyword">this</span>.flow.AddNewRequest(<span class="hljs-string">"Execute PayPal payment"</span>, payment);
                    <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Execute the payment.</p></div></div><div class="code"><div class="wrapper">                    <span class="hljs-keyword">var</span> executedPayment = payment.Execute(apiContext, paymentExecution);</div></div></div><div class="segment"><div class="code folded"><div class="wrapper marker"><span class="c1">                    // Ignore workflow code segment</span></div><div class="wrapper">                    <span class="hljs-comment">// Ignore workflow code segment</span>
                    <span class="hljs-preprocessor">#<span class="hljs-keyword">region</span> Track Workflow</span>
                    <span class="hljs-keyword">this</span>.flow.RecordResponse(executedPayment);
                    <span class="hljs-preprocessor">#<span class="hljs-keyword">endregion</span></span>
                }</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>Cleanup - Because there&#39;s a limit to the number of experience profile IDs you can create,
we&#39;ll delete the one that was created for this sample.</p></div></div><div class="code"><div class="wrapper">                WebProfile.Delete(apiContext, webProfileId);</div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>For more information, please visit <a href="https://developer.paypal.com/docs/api/">PayPal Developer REST API Reference</a>.</p></div></div><div class="code"><div class="wrapper">            }
        }
    }
}</div></div></div></div></body></html>